package com.house.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.house.common.Result;
import com.house.entity.*;
import com.house.service.*;
import com.house.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
public class LoginController {



    @Autowired
    UserService userService;

    @Autowired
    OrderService orderService;

    @Autowired
    RepairService repairService;

    @Autowired
    MessageService messageService;

    @Autowired
    HouseService houseService;



    /**
     * 登录页
     * @param request
     * @return
     */
    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        if(LoginUtil.checkAgentIsMobile(request)) {
            return "mobile/login";
        } else {
            return "pc/login";
        }
    }


    /**
     * 登出
     * @param request
     * @return
     */
    @GetMapping("/loginOut")
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 清空session 跳转首页
        request.getSession().setAttribute("user", null);
        response.sendRedirect("/");
    }


    /**
     * 注册页
     * @param request
     * @return
     */
    @GetMapping("/register")
    public String register(HttpServletRequest request) {
        if(LoginUtil.checkAgentIsMobile(request)) {
            return "mobile/register";
        } else {
            return "pc/register";
        }
    }


    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("login")
    @ResponseBody
    public Result login(User user, HttpServletRequest request) {
        User loginUser = userService.queryByUsername(user.getUsername());

        if(loginUser == null) {
            return Result.error("用户不存在！");
        }

        if(!SecureUtil.md5(user.getPassword()).equals(loginUser.getPassword())) {
            return Result.error("密码错误！");
        }

        request.getSession().setAttribute("user", loginUser);
        return Result.ok(loginUser);
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("register")
    @ResponseBody
    public Result register(User user, HttpServletRequest request) {
        Result result = userService.register(user);

        // 如果注册成功 直接变成登录状态
        if(result.isSuccess()) {
            request.getSession().setAttribute("user", result.getData());
        }
        return result;
    }


    /**
     * 个人中心页
     * @param model
     * @return
     */
    @GetMapping("/personal")
    public String personal(Model model, HttpServletRequest request) {

        // 我的订单
        List<Order> orders = orderService.list(Wrappers.<Order>query()
                .eq("status", "1")
                .eq("create_user", LoginUtil.getLoginUser().getId())
                .orderByDesc("create_time"));

        for (Order order : orders) {
            House house = houseService.getById(order.getHouseId());
            houseService.imageProcessor(house);
            order.setHouse(house);
        }

        // 我的故障报修
        List<Repair> repairs = repairService.list(Wrappers.<Repair>query()
                .eq("create_user", LoginUtil.getLoginUser().getId())
                .orderByDesc("create_time"));

        // 我的消息
        List<Message> messages = messageService.list(Wrappers.<Message>query()
                .eq("user_id", LoginUtil.getLoginUser().getId())
                .orderByDesc("create_time"));


        model.addAttribute("orders", orders);
        model.addAttribute("repairs", repairs);
        model.addAttribute("messages", messages);

        if(LoginUtil.checkAgentIsMobile(request)) {
            return "mobile/personal";
        } else {
            return "pc/personal";
        }
    }
}
